﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using Esav.ExceptionManagement;
using System.Threading;
using System.Configuration;
using System.Timers;

namespace RelateImageDocALS
{
    partial class RelateImageDoc : ServiceBase
    {
        #region private variables
                
            private Thread thdListnerThread;
            private Thread thdListnerThread2;
            private Thread thdListnerThread1;
            System.Timers.Timer timer = new System.Timers.Timer();

        #endregion

        #region properties
            
            protected internal Double _Frecuency;

            public Double Frecuency
            {
                get { return _Frecuency; }
                set { _Frecuency = value; }
            }

        #endregion
        
        public RelateImageDoc()
        {
            InitializeComponent();
        }

        static void Main()
        {
            System.ServiceProcess.ServiceBase[] ServicesToRun;
            ServicesToRun = new System.ServiceProcess.ServiceBase[] { new RelateImageDoc() };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
        }

        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            this._Frecuency = Double.Parse(ConfigurationSettings.AppSettings["Frecuency"].ToString());
            this.LoadService();

            timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
            timer.Interval = this.Frecuency;
            timer.Enabled = true;

        }

        protected override void OnStop()
        {
            timer.Enabled = true;
            thdListnerThread.Abort();
            thdListnerThread2.Abort();
            thdListnerThread1.Abort();
        }

        private void LoadService()
        {

            RelateImageComm objProcess = new RelateImageComm();
            thdListnerThread = new Thread(new ThreadStart(objProcess.DocumentRelated));
            thdListnerThread.Start();

            RelateImageCommDes objProcessDes = new RelateImageCommDes();
            thdListnerThread1 = new Thread(new ThreadStart(objProcessDes.DocumentRelated));
            thdListnerThread1.Start();

            RelateImageMaster objProcess2 = new RelateImageMaster();
            thdListnerThread2 = new Thread(new ThreadStart(objProcess2.DocumentRelated));
            thdListnerThread2.Start();
        }



        private void CurrentDomain_UnhandledException(Object Sender, UnhandledExceptionEventArgs Args)
        {
            Exception ex = (Exception)Args.ExceptionObject;
            ExceptionManager.Publish(ex);
            this.LoadService();
        }

        private void OnElapsedTime(Object source, ElapsedEventArgs e)
        {
            this.LoadService();
        }

        protected override void OnPause()
        {
            thdListnerThread.Suspend();
            thdListnerThread1.Suspend();
            thdListnerThread2.Suspend();
        }

        protected override void OnContinue()
        {
            thdListnerThread.Resume();
            thdListnerThread1.Resume();
            thdListnerThread2.Resume();
        }
    }
}
